package com.servlet;


import com.dao.imp.AddUpdDelDaoImp;
import com.dao.imp.FindUser;
import com.entity.User;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author Mathison
 * @Title: RegisterServlet
 * @Package: com.servlet
 * @Description: 注册验证
 * @date 2021/11/15 15:34
 **/

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String nickname = request.getParameter("nickname");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");

        String insertSql = "INSERT INTO onlineshop.users (user_password, user_nickname, user_email, user_phone) VALUES " +
                "(\'" +
                password+"\', \'" +
                nickname+"\', \'" +
                email+"\',\'" +
                phone+"\')";

//        System.out.println(insertSql);
        AddUpdDelDaoImp addUpdDelDaoImp = new AddUpdDelDaoImp();
        boolean registerStatus = addUpdDelDaoImp.addUpdDelGoods(insertSql);

        String json = "";

        if (registerStatus==true){

            String sql = "SELECT * FROM users where user_email = \'"+email+"\' AND user_phone = \'"+ phone+"\'";
            User user = FindUser.findUser(sql);
            json = "{status:0,user_id:"+user.getUser_id()+",msg:\'注册成功\'}";
        }else {
            json = "{status:1,msg:\'注册失败\'}";
        }

        JSONObject jsonObject = JSONObject.fromObject(json);
        response.getWriter().print(jsonObject);



    }
}
